/**
 * 
 */
package com.alstudio.ui.module.user;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.android.keyboard.ALKeyBoardManager;
import com.alstudio.view.dialog.AlertDialogCreator;
import com.alstudio.view.edittext.ALEditText;
import com.alstudio.view.edittext.ALEditText.OnALEditorActionListener;
import com.alstudio.view.listener.AlItemOnClickListener;
import com.loovee.imaohu.R;

/**
 * 举报用户
 * 
 * @author alonso lee
 * 
 */
public class ChargeUserActivity extends TitleBarActivity implements
		OnALEditorActionListener {

	private ALEditText mEt;
	private String jid;
	private String[] reason = null;
	private int selectPos = -1;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(getApplicationContext(), getClass().getSimpleName());
		initData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarActivityContentView(R.layout.charge_user);
		setViewOnclickListener(R.id.spinner, this);
		mEt = (ALEditText) findViewById(R.id.etContent);
		setCenterTextViewMessage(R.string.TxtChargeUser);
		rightUseImageButton(true);
		setRightButtonImageSrc(R.drawable.friend_title_icon_confirm);

//		setRightButtonBackground(R.drawable.friend_title_icon_confirm);
		setRightButtonClickListener(this);
		mEt.setALEditorActionListener(this);
		mEt.goneClearDrawable();
		getRightButton().setEnabled(false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		reason = getResources().getStringArray(R.array.report_type);
		jid = getIntent().getStringExtra("jid");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ALKeyBoardManager.dismissKeyBoard(this);
		super.onClick(v);
		switch (v.getId()) {
		case R.id.spinner:
			showSelectReasonDialog();
			break;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.TitleBarActivity#invokeRightBtnClick()
	 */
	@Override
	protected void invokeRightBtnClick() {
		// TODO Auto-generated method stub
		super.invokeRightBtnClick();
		invokeChargeUser();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onEditorAction
	 * ()
	 */
	@Override
	public void onEditorAction() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onContentClear
	 * ()
	 */
	@Override
	public void onContentClear() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#
	 * onContentChange(java.lang.String)
	 */
	@Override
	public void onContentChange(String content) {
		// TODO Auto-generated method stub
		boolean isFilled = content.length() != 0;
		getRightButton().setEnabled(isFilled);
		if (selectPos == -1) {
			getRightButton().setEnabled(false);
		}
	}

	/**
	 * 显示选择举报原因对话框
	 */
	private void showSelectReasonDialog() {
		ALKeyBoardManager.dismissKeyBoard(this);
		AlertDialogCreator dialog = AlertDialogCreator
				.createOneButtonWithListItemDialog(this, false, reason);
		dialog.setItemClickListener(new AlItemOnClickListener() {

			@Override
			public void OnClick(int position) {
				// TODO Auto-generated method stub
				selectPos = position;
				updateSelectReson();
			}
		});
		dialog.showDialog();
	}

	/**
	 * 显示举报原因
	 */
	private void updateSelectReson() {
		showTextMessage(R.id.reason, reason[selectPos]);
		if (TextUtils.isEmpty(mEt.getText().toString())) {
			getRightButton().setEnabled(false);
		} else {
			getRightButton().setEnabled(true);
		}
	}

	/**
	 * 发送举报请求
	 */
	private void invokeChargeUser() {

		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALKeyBoardManager.dismissKeyBoard(this);
			ALUiXmppRequestCreator.postALUiXmppChargeUser(jid,
					reason[selectPos], mEt.getText().toString().trim());
			showFinishActivityAlertDialog(getString(R.string.TxtChargeSuccess));
		}
	}
}
